<?php
    include('protect2.php');

	include("connect.php");
	$kodemkx=$_GET["tolkodemk"];
	$nrpx=$_GET["tolnrp"];
	$pilih=mysql_select_db("uwika_krs",$conn);
	
	// CEK DOBEL PENDAFTARAN DALAM KRS
	$sqlstr="select nrp from registrasi where nrp='$tolnrp' and kodemk='$kodemkx' and periode='$sel_periode' and sesi='$sel_sesi'";
	$hasil=mysql_query($sqlstr);
	list($nrp)=mysql_fetch_row($hasil);
	if (!empty($nrp)) {
			echo " <SCRIPT> alert(\"Mata kuliah yang akan anda daftarkan sudah ada dalam KRS mahasiswa !\") </SCRIPT> ";
		    mysql_close($conn);
			echo "<script type=\"text/javascript\">";
			echo "history.go(-1)";
			echo "</script>";
			exit;
		}

			
	//CEK STATUS PENDAFTARAN -> Baru / Ulang
	$sqlstr="select kodemk from sks where kodemk='$kodemkx' and nrp='$nrpx'";
	$hasil=mysql_query($sqlstr);
	list($statusmk)=mysql_fetch_row($hasil);
	if (!empty($statusmk)) {
		$status_mk='ULANG';
	} else {
		$status_mk='BARU';
	}

	//AMBIL JUMLAH SKS DALAM MASTER MATA KULIAH
	$sqlstr="select sks from mk111 where kodemk='$kodemkx'";
	$hasil=mysql_query($sqlstr);
	list($sksx)=mysql_fetch_row($hasil);
	
	//AMBIL JADWAL HARI MATA KULIAH
	$sqlstr="select hari,mulaipukul,selesaipukul from penawaran where kodemk='$kodemkx'";
	$hasil=mysql_query($sqlstr);
	list($dftr_hari,$dftr_mulaipukul,$dftr_selesaipukul)=mysql_fetch_row($hasil);

	//TAMBAHKAN MATA KULIAH KE DALAM KRS MAHASISWA
	$sqlstr="select kodemk from registrasi where nrp='$nrpx' and kodemk='$kodemkx' and periode='$sel_periode' and sesi='$sel_sesi'";
	$hasil=mysql_query($sqlstr);
	list($kodemk)=mysql_fetch_row($hasil);
	if (empty($kodemk)) {
		$tgl=date("Y-m-d");
		$sqlstr="insert into registrasi (nrp,kodemk,periode,status,sesi,tanggal,sks,hari,mulaipukul,selesaipukul) values ('$nrpx','$kodemkx','$sel_periode','$status_mk','$sel_sesi','$tgl','$sksx','$dftr_hari','$dftr_mulaipukul','$dftr_selesaipukul')";
		$hasil=mysql_query($sqlstr);
		mysql_close($conn);
		echo "<script type=\"text/javascript\">";
		echo "history.go(-1)";
		echo "</script>";
		exit;
	} else {
		echo " <SCRIPT> alert(\"Mata kuliah tersebut sudah terdaftar dalam KRS mahasiswa  !\") </SCRIPT> ";
	    mysql_close($conn);
		echo "<script type=\"text/javascript\">";
		echo "history.go(-1)";
		echo "</script>";
		exit;
	}
?>